import inpection from 'components/dorm/children/inspector.vue'
import dormStudents from 'components/component/dormStudents.vue'
export const dormInfoMixin = {
  data () {
    return {
      tableData: [],
      pagenum: 1,
      total: 0
    }
  },
  components: {
    inpection,
    dormStudents
  },
  methods: {
    // 请求数据
    async getDormLeader (tower, event, pagenum) {
      const { data: res } = await this.$axios.get('/student/findDormLeader', {
        params: {
          tower: !tower.label ? tower : tower.label,
          pagenum: !pagenum ? pagenum = 1 : pagenum
        }
      })
      if (res.meta.code !== 200) {
        this.$message(res.meta.msg)
        this.tableData = []
      } else {
        const handle = []
        this.total = res.total
        res.result.map(valid => {
          if (!valid.dormLeader) {
            valid.dormLeader = '该宿舍该没有宿舍长'
            if (!valid.score) {
              valid.score = 0
              handle.push(valid)
            } else {
              handle.push(valid)
            }
          } else {
            if (!valid.score) {
              valid.score = 0
              handle.push(valid)
            } else {
              valid.score = Number(valid.score)
              handle.push(valid)
            }
          }
        })
        this.tableData = handle
      }
    },
    // 改变宿舍默认床位数，默认是6
    changeDormBed (row) {
      // console.log(row)
      this.$prompt('请输入修改容量', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(async ({ value }) => {
        const { data: res } = await this.$axios.get('/student/changeBed', {
          params: {
            id: row._id,
            spare: value
          }
        })
        // console.log(res)
        if (res.meta.code === -1) {
          this.$message.error(res.meta.msg)
        } else {
          this.$message.success(res.meta.msg)
          this.getDormLeader(row.tower, null, this.pagenum)
        }
        // console.log(res)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消修改'
        })
      })
    },
    // 点击页数时
    pagenumChange (currpagenum) {
      this.pagenum = currpagenum
      if (window.localStorage.getItem('role') === 'inspector') {
        this.getDormLeader(window.localStorage.getItem('towers'), null, currpagenum)
      } else {
        this.getDormLeader(this.tableData[0].tower, null, currpagenum)
      }
    },
    // 修改宿舍长
    changeDormLeader (row) {
      // console.log(row)
      this.$prompt('请输入宿舍长名字', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(async ({ value }) => {
        const { data: res } = await this.$axios.get('/student/changeDormLeader', {
          params: {
            id: row._id,
            dormLeader: value,
            tower: row.tower,
            dorm: row.dorm
          }
        })
        // console.log(res)
        if (res.meta.code === -1) {
          this.$message.error(res.meta.msg)
        } else {
          // console.log(111111)
          this.getDormLeader(row.tower, null, this.pagenum)
          this.$message.success(res.meta.msg)
        }
        // console.log(res)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消修改'
        })
      })
    },
    // 获取当前宿舍的所有成员
    getCurrDormStudents () {
      // console.log(this.$refs.dormStudentRef)
      if (this.$refs.dormStudentRef.length) {
        this.$refs.dormStudentRef[0].getCurrStudentsInfo()
      } else {
        this.$refs.dormStudentRef.getCurrStudentsInfo()
      }
    }
  }
}
